<? ob_start(); ?>
<?php
class Product extends MY_Controller
{
    public $filed_items;

    function __construct()
    {
        parent::__construct();
        $this->load->model('product_model');
        $this->load->model('category_model');
        $this->load->config('home');
        $this->load->helper('text');
        $this->load->helper('main_helper');
        $this->data['cur_pr'] = 'current';
        //$this->output->enable_profiler();
    }

    function index()
    {
        $numberget = 15;
        $this->load->library('MY_Paginationhome');
        $this->data = null;
        if (empty($_GET['page']) and empty($_GET['keyword'])) {
            $this->session->unset_userdata("keyword");
        }
        if (isset($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $this->session->set_userdata('keyword', $keyword);
            $keyword = $this->session->userdata('keyword');
        } else  $keyword = '';

        if (isset($_GET['page'])) {
            $keyword = $this->session->userdata('keyword');
            $page = $_GET['page'];
        } else $page = 1;

        $start = ((int)$page - 1) * $numberget;
        $start = $start > 0 ? $start : 0;
        $this->data['products'] = $this->product_model->searchAjax($total, $keyword, $start, $numberget);
        //echo $this->db->last_query();die;
        $config['total_rows'] = $total; //tong so record
        $config['per_page'] = $numberget; //xac dinh so record o moi trang
        $config['num_links'] = 20;
        $config['cur_page'] = $start;
        //$config['uri_segment'] = 4;//xac dinh segment chua page number
        $config['first_link'] = 'first';
        $config['last_link'] = 'last';
        $config['next_link'] = 'next';
        $config['prev_link'] = 'pres';
        $config['full_tag_open'] = ''; //tao the div cho link phan trang
        $config['full_tag_close'] = '';

        $this->my_paginationhome->initialize($config);
        $this->data['paging'] = $this->my_paginationhome->create_links();
        $this->data['all'] = 'current2';
        $this->data['cur_pr'] = 'current';
        $this->load->view('backend/header', $this->data);
        $this->load->view('backend/product/home', $this->data);
        $this->load->view('backend/footer');
    }

    function insert()
    {
        if ($post = $this->input->post()) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->product_model->getRules());
            if ($this->form_validation->run() === true) {
                $config['upload_path'] = './assets/product_images/product/0/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = true; //doi ten anh
                //	$config['max_size']	= '100';
                //	$config['max_width']  = '1024';
                //	$config['max_height']  = '768';

                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('product_image')) {
                    $this->load->view('backend/product/form');
                } else {
                    $data_upload = $this->upload->data();

                    $data = $this->input->post();
                    //echo '<pre>'; var_dump($data); echo '</pre>';die;
                    $data['date'] = time();
                    $data['product_image'] = '0/' . $data_upload['file_name'];

                    $this->product_model->save($data);
                    $this->session->set_flashdata('insert', 'ban da them moi 1 san pham ten la ' . $post['product_name']);
                    redirect('admin/product');
                }

            }
        }
        $this->data['insert'] = 'current2';
        $this->load->view('backend/header', $this->data);
        $this->load->view('backend/product/form');
        $this->load->view('backend/footer');

    }

    function update($product_id)
    {
        if ($post = $this->input->post()) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->product_model->getRules());
            if ($this->form_validation->run() === true) {
                $config['upload_path'] = './assets/product_images/product/0/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = true;

                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('product_image')) {
                    $this->load->view('backend/product/form');
                } else {
                    $data_upload = $this->upload->data();

                    $data = $this->input->post();
                    $data['product_image'] = '0/' . $data_upload['file_name'];

                    //echo '<pre>'; var_dump($data); echo '</pre>';die;
                    $this->product_model->save($data, $product_id);
                    //echo $this->db->last_query();die;
                    $this->session->set_flashdata('edit', 'ban da sua thanh cong san pham ten la ' . $post['product_name']);
                    redirect('admin/product');
                }
            }
        }
        $this->data['edit'] = 'current2';
        $this->data['product'] = $this->product_model->getOne($product_id);

        $this->load->view('backend/header', $this->data);
        $this->load->view('backend/product/form');
        $this->load->view('backend/footer');
    }

    function delete($product_id)
    {
        $this->product_model->delete($product_id);
        $this->product_model->delProCate($product_id);
        $img = $this->product_model->getImgById($product_id);
        @unlink('assets/product_images/product/' . $img->product_image);
        redirect('admin/product');
    }

    function searchAjax()
    {
        $this->load->library('MY_Paginationhome');
        $this->data = null;
        if (empty($_GET['page']) and empty($_GET['keyword'])) {
            $this->session->unset_userdata("keyword");
            $this->session->unset_userdata("numberget");
        }
        if (isset($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $this->session->set_userdata('keyword', $keyword);
            $keyword = $this->session->userdata('keyword');
        } else  $keyword = '';
        if (isset($_GET['number'])) {
            $numberget = $_GET['number'];
            $this->session->set_userdata('numberget', $numberget);
            $numberget = $this->session->userdata('numberget');
        } else $numberget = 10;
        if (isset($_GET['page'])) {
            $numberget = $this->session->userdata('numberget');
            $keyword = $this->session->userdata('keyword');
            $page = $_GET['page'];
        } else $page = 1;

        $start = ((int)$page - 1) * $numberget;
        $start = $start > 0 ? $start : 0;
        $this->data['products'] = $this->product_model->searchAjax($total, $keyword, $start, $numberget);
        $config['total_rows'] = $total; //tong so record
        $config['per_page'] = $numberget; //xac dinh so record o moi trang
        $config['num_links'] = 20;
        $config['cur_page'] = $start;
        //$config['uri_segment'] = 4;//xac dinh segment chua page number
        $config['first_link'] = 'first';
        $config['last_link'] = 'last';
        $config['next_link'] = 'next';
        $config['prev_link'] = 'pres';
        $config['full_tag_open'] = ''; //tao the div cho link phan trang
        $config['full_tag_close'] = '';

        $this->my_paginationhome->initialize($config);
        $this->data['paging'] = $this->my_paginationhome->create_links();

        $this->load->view('backend/header', $this->data);
        $this->load->view('backend/product/home', $this->data);
        $this->load->view('backend/footer');
    }

    function logOut()
    {
        $this->session->unset_userdata('user_role');
        redirect('admin/user/login');
    }

    function updateStatus($product_id = 0, $status = 0)
    {
        $product_id = intval($product_id);
        $status = intval($status);

        $this->product_model->update_item_status($product_id, $status);

    }
}